import torch

from algorithms.base.nsga2 import NSGA2
from indicators import Indicator


class IGDp(Indicator):
    @classmethod
    def value(cls, pop_obj, pf):
        [Nr, M] = pf.shape
        pop_obj = NSGA2.get_best(pop_obj)
        [N, _] = pop_obj.shape
        delta = torch.zeros(Nr, dtype=torch.double)
        zeros = torch.zeros((N, M), dtype=torch.double)
        for i in range(Nr):
            delta[i] = torch.max(pop_obj - pf[i], zeros).norm(p=2, dim=1).min()
        return delta.mean()
